﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FlashGames
{
    public class Site : Object
    {
        private static int nextId;
        public int ID { get; private set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public string GameBaseUrl { get; set; }
        public string ImageUrl { get; set; }
        public Image Image { get; set; }
        public string Description { get; set; }
        public bool HasCategories { get; set; }
        public string CategoryRegex { get; set; }
        public string SubCategoryRegex { get; set; }
        public string GameRegex { get; set; }
        public string GameFileRegex { get; set; }
        public bool HasGames { get; set; }
        public bool HasSubCategories { get; set; }
        public bool GamesDiscovered { get; set; }
        public bool CategoryDiscovered { get; set; }
        private List<Category> _categories = new List<Category>();
        private List<Game> _games = new List<Game>();
        

        public virtual List<Game> Games
        {
            get { return _games; }
            set 
            { 
                _games = value;
                GamesDiscovered = true;
            }
        }

        public virtual List<Category> Categories
        {
            get { return _categories; }
            set 
            { 
                _categories = value;
            }
        }

        public Site()
        {
            ID = Site.nextId;
            Site.nextId++;

        }
        
        public virtual List<Category> LoadCategories()
        {
            Util.baseUrl = this.Url;

            if (HasCategories)
            {
                string webData = Util.GetWebData(this.Url);
                try
                {
                  Match m = Regex.Match(webData, this.CategoryRegex.ToLower(), RegexOptions.Singleline | RegexOptions.IgnoreCase);
                  if (m.Groups["title"].Success && m.Groups["url"].Success)
                  {
                    while (m.Success)
                    {
                      Category category = new Category();
                      category.Name = m.Groups["title"].Value;
                      category.ImageUrl = m.Groups["imageUrl"].Value;
                      //if (Util.GetThumbFormCache(category.Name) != null)
                      //    category.Image = Util.GetThumbFormCache(category.Name);
                      //Util.DownloadThumb(category.ImageUrl, category.Name);
                      category.Url = m.Groups["url"].Value;
                      category.ParentSite = this;
                      if (!string.IsNullOrEmpty(this.SubCategoryRegex))
                        category.HasSubCategories = true;
                      if (!string.IsNullOrEmpty(this.CategoryRegex))
                        category.HasGames = true;
                      this.Categories.Add(category);
                      this.CategoryDiscovered = true;

                      m = m.NextMatch();
                    }
                  }
                }
                catch
                {

                }
            }
            else LoadGames();
            return Categories;
        }

        public virtual MediaPortal.GUI.Library.GUIListItem ToGUIListItem()
        {
            MediaPortal.GUI.Library.GUIListItem Item = new MediaPortal.GUI.Library.GUIListItem();
            Item.Label = this.Name;
            Item.Path = this.Url;
            //Item.Thumbnail = this.Image;
            Item.AlbumInfoTag = this;
            return Item;
        }

        public virtual List<Game> LoadGames()
        {
            Util.baseUrl = this.Url;

            if (HasGames)
            {
                string webData = Util.GetWebData(this.Url);
                try
                {
                  Match m = Regex.Match(webData, this.GameRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline);

                  while (m.Success)
                  {
                    Game game = new Game();
                    game.Name = m.Groups["title"].Value;
                    game.ImageUrl = m.Groups["imageUrl"].Value;
                    Util.DownloadThumb(game.ImageUrl, game.Name);
                    game.Url = m.Groups["url"].Value;
                    game.ParentSite = this;
                    this.Games.Add(game);
                    m = m.NextMatch();
                  }
                }
                catch
                {

                }
            }
            return Games;
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
